<?php

namespace App\Models;
class Balance extends Base
{
    const TYPE_VIP = 3;
    const TYPE_COOKBOOK_REC = 2;
    const TYPE_COOKBOOK = 1;
    const TYPE_WITHDRAW = 4;
    const TYPE_MALL = 5;
    const TYPE_ADV = 6;
    const TYPE_COOKBOOK_VIDEO = 7;

    protected $appends = ['amount_yuan', 'balance_yuan', 'paid_total_amount_yuan', 'platform_commission_yuan'];

    public function order() {
        return $this->belongsTo(Order::class, 'order_id');
    }

    public function getAmountYuanAttribute() {
        return format_yuan($this->amount);
    }

    public function getPlatformCommissionYuanAttribute() {
        return format_yuan($this->platform_commission);
    }

    public function getPaidTotalAmountYuanAttribute() {
        return format_yuan($this->paid_total_amount);
    }

    public function getBalanceYuanAttribute($balance) {
        return format_yuan($this->balance);
    }

    public static function getTotalIncome($merchant_id, $benefit_user = 1) {
        $search_key = $benefit_user == 1 ? 'merchant_id' : 'parent_user_id';
        return Balance::where('type', '!=', self::TYPE_WITHDRAW)->where('benefit_user', $benefit_user)->where($search_key, $merchant_id)->sum('amount') ?? 0;
    }

    public function user() {
        return $this->belongsTo(User::class, 'user_id');
    }


    public function adv_sign_up() {
        return $this->belongsTo(AdvSignUp::class, 'order_id');
    }
}
